<?php
apf_require_controller("Abstract");
apf_require_class("Bll_Portal_Message");
class Ajax_MessageController extends AbstractController {

    public function handle_request() {
        $bll = new Bll_Portal_Message();
        $params = $this->request->get_parameters();

        if($params['type']=='add'){//添加，
            $status = 0;
            if(trim($params['content']) &&$params['content']!='亲！把绽放心语码这里！一同见证绽放哟~~限50字以内'){
                $user_info = $this->get_user_info();
                $content = htmlspecialchars(trim($params['content']));
                $data = array(
                        'user_id'=>$this->get_user_id(),
                        'content'=>$content,
                        'user_name'=>$user_info['user_name'],
                        );
                $res = $bll->add_message($data);
                if($res){
                    $status = 1;
                }
            }
            $msg = $status ? "添加成功！":"添加失败！";
            $this->json_encode($status,$msg);
            exit;
        }elseif($params['type']=='get'){//获取
            $list = $bll->get_all_messages();
            $tmp = array();
            if(!empty($list)){
                foreach($list as $val){
                    $tmp[$val['user_id']] = $val;
                }
            }
            $this->json_encode(1,$tmp);
            exit;
        }
        $this->show_tips();
    }
    /**
     * 初始化列表
     * @return array
     */
    private function _init_data(){
        $max_width = 940;
        $max_height = 540;
        $one_wihe = 20;
        $start_with  = 10;
        $start_heght = 10;
        $colum_nums = $max_width/$one_wihe;//47 = 23+1+23
        $row_nums = $max_height/$one_wihe; //27 = 13+1+13
        $result = array();
        for($colum=0;$colum < $colum_nums;$colum++){
            for($row=0;$row < $row_nums;$row++){
                $result[]=array(
                        'left'=>$start_with+$colum*$one_wihe,
                        'top' =>$start_heght+$row*$one_wihe,
                        'user_id'=>'',
                        'content'=>'',
                        'user_name'=>''
                        );
            }
        }
        return $result;
    }
    /**
     * 随机点
     */
    private function _get_random_data($message,$init_data){
        $init_data_key = array_keys($init_data);
        $count = count($init_data);
        $middle = $count%2?intval($count/2)+1:$count/2;
        $min = 0;
        $max = $count-1;
        foreach($message as $mes){
            if($mes['user_id'] == $this->get_user_id()){
                $i = $middle;
            }else{
                $i = $this->_get_random($init_data, $min, $max, $middle);
            }
            $init_data_key = $this->_init_key_data($i,$init_data_key);
            $init_data[$i]['user_id'] = $mes['user_id'];
            $init_data[$i]['content'] = $mes['content'];
            $init_data[$i]['user_name'] = $mes['user_name'];
        }
        return $init_data;
    }
    private function _init_key_data($i,$init_data_key){
        $result = array();
        foreach($init_data_key as $val){
            if($i != $val){
                $result[] = $val;
            }
        }
        return $result;
    }
    //随着数据量的增加，速度越来越慢
    private function _get_random($init_data,$middle,$init_data_key,$num=0){echo $middle;exit;
        $i = 0;
        if($num<4){//中间区域优先
            $w=rand(7, 40);//47
            $h=rand(7, 20);//27
            $i = $w*$h;
        }else{//空块，随机
            $temp_key = array_keys($init_data_key);
            $random = rand(min($temp_key), max($temp_key));
            $i = $init_data_key[$random];
        }
        if($init_data[$i]['user_id'] || $middle==$i){
            $num++;
            $i=$this->_get_random($init_data,$middle,$init_data_key,$num);
        }
        return $i;
    }
}
